home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / checkbox / registries / hw.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  4.0 KB  |  127 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. import logging
  7. from checkbox.lib.cache import cache
  8. from checkbox.properties import String
  9. from checkbox.registries.command import CommandRegistry
  10. from checkbox.registries.data import DataRegistry
  11. from checkbox.registries.map import MapRegistry
  12.  
  13. class DeviceRegistry(DataRegistry):
  14.     '''Registry for HW device information.
  15.  
  16.     Each item contained in this registry consists of the properties of
  17.     the corresponding HW device.
  18.     '''
  19.     
  20.     def items(self):
  21.         items = []
  22.         lines = []
  23.         id = None
  24.         status = None
  25.         depth = None
  26.         for line in self.split('\n'):
  27.             if not line:
  28.                 continue
  29.             
  30.             match = re.match('(\\s+(\\*-)?)(.+)', line)
  31.             if not match:
  32.                 raise Exception, 'Invalid line: %s' % line
  33.             match
  34.             space = len(match.group(1))
  35.             if depth is None:
  36.                 depth = space
  37.             
  38.             if space > depth:
  39.                 lines.append(line)
  40.                 continue
  41.             if match.group(2) is not None:
  42.                 if id is not None:
  43.                     value = DeviceRegistry('\n'.join(lines))
  44.                     lines = []
  45.                     items.append((id, value))
  46.                     items.append(('status', status))
  47.                 
  48.                 node = match.group(3)
  49.                 match = re.match('([^\\s]+)( [A-Z]+)?', node)
  50.                 if not match:
  51.                     raise Exception, 'Invalid node: %s' % node
  52.                 match
  53.                 id = match.group(1)
  54.                 status = match.group(2)
  55.                 continue
  56.             (key, value) = match.group(3).split(': ', 1)
  57.             key = key.replace(' ', '_')
  58.             values = value.split(' ')
  59.             if key == 'product':
  60.                 match = re.search('(.*) \\[[0-9A-F]{1,4}:([0-9A-F]{1,4})\\]$', value)
  61.                 if match:
  62.                     value = match.group(1)
  63.                     items.append(('product_id', int(match.group(2), 16)))
  64.                 
  65.             elif key == 'vendor':
  66.                 match = re.search('(.*) \\[([0-9A-F]{1,4})\\]$', value)
  67.                 if match:
  68.                     value = match.group(1)
  69.                     items.append(('vendor_id', int(match.group(2), 16)))
  70.                 
  71.             elif key.endswith('s'):
  72.                 value = values
  73.             elif _[1]:
  74.                 index = 1
  75.                 for value in values[1:]:
  76.                     if '=' not in value:
  77.                         values[index - 1] += ' %s' % values.pop(index)
  78.                         continue
  79.                     []
  80.                     index += 1
  81.                 
  82.                 value = dict((lambda .0: for v in .0:
  83. v.split('=', 1))(values))
  84.                 value = MapRegistry(value)
  85.             
  86.             items.append((key, value))
  87.         
  88.         if lines:
  89.             value = DeviceRegistry('\n'.join(lines))
  90.             items.append((id, value))
  91.         
  92.         return items
  93.  
  94.     items = cache(items)
  95.  
  96.  
  97. class HwRegistry(CommandRegistry):
  98.     '''Registry for HW information.
  99.  
  100.     Each item contained in this registry consists of the hardware id as
  101.     key and the corresponding device registry as value.
  102.     '''
  103.     command = String(default = 'lshw -numeric 2>/dev/null')
  104.     version = String(default = 'lshw -version 2>/dev/null')
  105.     
  106.     def __str__(self):
  107.         logging.info('Running command: %s', self.version)
  108.         version = os.popen(self.version).read().strip()
  109.         numbers = version.split('.')
  110.         if len(numbers) == 3 and numbers[0] == 'B' and int(numbers[1]) == 2 and int(numbers[2]) < 13:
  111.             self.command = self.command.replace(' -numeric', '')
  112.         
  113.         return super(HwRegistry, self).__str__()
  114.  
  115.     __str__ = cache(__str__)
  116.     
  117.     def items(self):
  118.         lines = self.split('\n')
  119.         key = lines.pop(0)
  120.         value = DeviceRegistry('\n'.join(lines))
  121.         return [
  122.             (key, value)]
  123.  
  124.     items = cache(items)
  125.  
  126. factory = HwRegistry
  127.